package com.jf.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jf.springcloud.handler.GlobalBlockHandler;
import com.jf.springcloud.handler.GlobalFallbackHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-02-28
 * Time:11:38
 */
@RequestMapping("/sentinelResource")
@RestController
public class SentinelResourceController {
    private int num1 = 0;
    private int num2 = 0;

    @RequestMapping("/globalBlock1")
    @SentinelResource(value = "globalBlock1", blockHandler = "blockHandler1", blockHandlerClass = GlobalBlockHandler.class)
    public String globalBlock1() {
        return "global block test 1";
    }

    @RequestMapping("/globalBlock2")
    @SentinelResource(value = "globalBlock2", blockHandler = "blockHandler2", blockHandlerClass = GlobalBlockHandler.class)
    public String globalBlock2() {
        return "global block test 2";
    }

    @RequestMapping("/globalFallback")
    @SentinelResource(value = "globalFallback", fallback = "fallback", fallbackClass = GlobalFallbackHandler.class)
    public String globalFallback() {
        if (num1++ % 2 == 0) {
            int i = 1 / 0;
        }
        return "global fallback test";
    }
    @RequestMapping("/globalFallback2")
    @SentinelResource(value = "globalFallback2",
            fallback = "fallback2", fallbackClass = GlobalFallbackHandler.class,
            exceptionsToIgnore = {RuntimeException.class})
    public String globalFallback2() {
        if (num2++ % 2 == 0) {
            int i = 1 / 0;
        }
        return "global fallback test 2";
    }
}
